<?php
require_once('TVB/pms/pdes/action/InfoAddressAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoAwardAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoBiographyAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoGeneralAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoPageantAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoProductAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoQnaAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoRecipeAddAction.class.php');
require_once('TVB/pms/pdes/action/InfoSongAddAction.class.php');

class PdesSwitchAction extends BaseAction 
{

	public $type;
	public $act;
	public $step;
	public $vid;

	public function getPermissionItems(){
		$items = parent::getPermissionItems();
		$items[] = 'Liberal';
		return $items;
	}

	public function execute(){

		$this->step = !empty($this->step) ? $this->step : 1;
		$this->setData("act", $this->act);
		$this->setData("type", $this->type);
		$this->setData("step", $this->step);
		$this->setData("check_used", 0);

		try
		{
			switch ($this->type)
			{
			case "programme":
				$this->init_programme_page();
				break;
			case "song":
				$this->init_object_property('song');
				break;
			case "address":
				$this->init_object_property('address');
				break;
			case "biography":
				$this->init_object_property('biography');
				break;
			case "chapter_marking":
				$this->init_chapter_page();
				break;
			case "recipe":
				$this->init_object_property('recipe');
				break;
			case "award":
				$this->init_object_property('award');
				break;
			case "product":
				$this->init_object_property('product');
				break;
			case "qa":
				$this->init_object_property('qa');
				break;
			case "general_info":
				$this->init_object_property('general_info');
				break;
			case 'pageant':
				$this->init_object_property('pageant');
				break;
			}

			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("Get {$this->type} Fails: " . var_export($e, 1));

			return 'system-error';
		}
	}    
	
	/**
	 * get info init data
	 */
	private function init_object_property($key){
		$action_name = AC::$infoAction[$key];
		$action = new $action_name();
		$object_name = AC::$infoType[$key];
		$object = new $object_name();
		$ref = new ReflectionObject($action);
		$props = $ref->getProperties();
		foreach ($props as $prop)
		{
			if ($prop->isPublic())
			{
				 $prop_name = $prop->getName();
				 $object->$prop_name = '';
			}
		}
		if ($key == 'biography')
		{
			$object->origin_country = '';
			$object->origin_province_state_prefecture = '';
		}

		$this->setData($key, $object);
		$this->setData('publishLog',null);
	}

	/**
	 * get programme company code list
	 */
	private function init_programme_page()
	{
		$company_code = new company_code();
		$company_code_list = $company_code->get();
		$company_code_arr  = array();
		foreach ($company_code_list as $company_code)
		{
			$company_code_arr[$company_code->code] = $company_code->system_label;
		}
		$this->setData("company_code_list", $company_code_arr);
	}

	/**
	 * get video detail
	 */
	private function init_chapter_page()
	{
		$video = new video();
		$video_detail = $video->getDetails($this->vid);
		$this->setData("video", $video_detail);

		//programme detail
		$programme = new programme();
		$programme_detail = $programme->getDetails($video_detail['programme_id']);
		$programme_name = explode("\n\t",$programme_detail->mix_programme_name);
		$programme_detail->zh_programme_name = $programme_name[0];
		$programme_detail->en_programme_name = $programme_name[1];
		$this->setData("programme", $programme_detail);

		//episode detail 
		$episode = new episode();
		$episode_id = episode_video::getEpisodeID($this->vid);
		$episode_detail = $episode->getDetails($episode_id);
		$title = explode("\n\t",$episode_detail->mix_title);
		$episode_detail->title = $title[0];
		$episode_detail->title_en = $title[1];
		$this->setData("episode", $episode_detail);

		//allow tags
		$allow_tag = CommonUtils::get_allow_tags("chapter");
		$tag_type = new tag_type();
		$allow_tag_list = $tag_type->findByInSql($allow_tag);
		$tag_arr = array();
		foreach ($allow_tag_list as $tag)
		{
			$tag_arr[$tag->id] = array("id"=>$tag->id,"name"=>$tag->system_label,'restrict'=>$tag->restricted);
		}
		$this->setData("allow_tag", $tag_arr);

	}
}
?>
